package com.homegoods.classify.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.classify.R;
import com.homegoods.classify.adapter.RecycleViewAdapter;
import com.homegoods.classify.databinding.ClassifyZpFragmentBinding;
import com.homegoods.classify.persenter.ZPClassifyPresenter;
import com.homegoods.classify.view.ClassifyView;
import com.jakewharton.rxbinding2.view.RxView;
import com.kedll.library.fragment.KBaseFragment;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * add shibo.zheng
 * 19/06/18
 * 宅品直营分类主页面
 */
@Route(path = ARouterPath.PATH_FRAGMENT_CLASSIFY)
public class ZPClassifyFragment extends KBaseFragment<ClassifyZpFragmentBinding, ZPClassifyPresenter> implements ClassifyView {
    private RecyclerView top_lv;    //左边分类导航栏
    private ViewPager mainViewPager;    //定义碎片的容器

    private RecycleViewAdapter viewAdapter;
    private int index_pos = 0;

    @Override
    protected void initEvent(Bundle savedInstanceState) {
        top_lv = mBinding.topLv;
        //设置RecyclerView的布局管理
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL,false);
        top_lv.setLayoutManager(linearLayoutManager);

        //设置RecyclerView的Item间分割线
//        top_lv.addItemDecoration(new DividerltemDecoration(getContext(), DividerltemDecoration.VERTICAL_LIST));
        
        //搜索
        RxView.clicks(mBinding.etSearch)
                .compose(bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> ARouter.getInstance()
                        .build(ARouterPath.PATH_SEARCH)
                        .navigation());

        mPresenter.getClassifyModel();
    }

    @Override
    protected ZPClassifyPresenter getPresenter() {
        return new ZPClassifyPresenter(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.classify_zp_fragment;
    }

    @Override
    public void initZPViewPager(List<Fragment> fragments, List<String> titles) {

        mainViewPager = mBinding.childContainerLl;
//        initClassifyDatas();
        viewAdapter = new RecycleViewAdapter(getContext(), titles, index_pos);
        top_lv.setAdapter(viewAdapter);

        //设置Item点击事件
        viewAdapter.setOnItemClickListener((view, position) -> {
            viewAdapter.position = position;
            viewAdapter.notifyDataSetChanged();
            //点击按钮fragment跳转
            mainViewPager.setCurrentItem(position, false);
        });

        mainViewPager.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });
    }

    @Override
    public void showLoad(String msg) {

    }

    @Override
    public void hideLoad() {

    }
}
